package com.lxy.service.impl;

import com.lxy.config.MinioProperties;
import com.lxy.service.MinioService;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Service
public class MinioServiceImpl implements MinioService {
    @Autowired
    MinioProperties minioProperties;

    @Autowired
    MinioClient minioClient;

    @Override
    public String upload(String bucketName, MultipartFile file) {

        //获取时间2025-04-15
        String dataTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //获取uuid
        String uuid = UUID.randomUUID().toString();
        //获取文件名
        String originalFileName = file.getOriginalFilename();
        //获取文件的后缀名
        String extension = originalFileName.substring(originalFileName.lastIndexOf("."));
        String newFileName = uuid + extension;
        //设置文件存储的目录
        String fileName = dataTime + "/" + newFileName;
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName) //文件上传到哪个存储桶。
                    .object(fileName)   //上传文件在存储桶中的对象名称。
                    .stream(file.getInputStream(),file.getSize(),-1)    //-1 表示让 MinIO 客户端自行决定分块大小。
                    .contentType(file.getContentType())
                    .build());
            String url = minioProperties.getEndpoint() + "/" + bucketName + "/" + fileName;
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }


    @Override
    public ResponseEntity<InputStreamSource> download(String url) {
        String filename = url.split(minioProperties.getBucketName()+"/")[1];
        try {
            GetObjectArgs getObjectArgs=  GetObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .object(filename)
                    .build();
            InputStream inputStream = minioClient.getObject(getObjectArgs);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename="+ filename.split("/")[1])
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .body(new InputStreamResource(inputStream));
        } catch (Exception e) {
            throw new RuntimeException("minio文件下载失败");
        }
    }
}
